/**
 *  Copyright (C) 2008-2013  Telosys project org. ( http://www.telosys.org/ )
 *
 *  Licensed under the GNU LESSER GENERAL PUBLIC LICENSE, Version 3.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *          http://www.gnu.org/licenses/lgpl.html
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.telosys.util;

/**
 * Set of functions to cut a string in parts, using a given separator character
 * 
 * @author Laurent GUERIN
 * 
 *
 */
public class Parts
{
    //--------------------------------------------------------------------------------
    /**
     * Returns an array of separators positions 
     * @param str the string where to search the separators
     * @param cSeparator the separator character
     * @param iMaxSep the maximum number of separators
     * @return the separators positions
     */
    public static int[] getSeparators( String str, char cSeparator, int iMaxSep )
    {
        if ( str == null || iMaxSep < 1 )
        {
            return new int[0] ; // No separator
        }
        
        byte[] s = str.getBytes();
        int[] sep = new int[iMaxSep];
        int iCount = 0 ;
        boolean bInString = false ;
        int iCurrent = 0 ;
        for ( int i=0 ; i < s.length ; i++ )
        {
            if ( s[i] == cSeparator ) // Separator found
            {
                if ( bInString != true )
                {
                    if ( iCurrent < sep.length )
                    {
                        sep[iCurrent] = i ;
                        iCurrent++ ;
                        iCount++;
                    }
                }
            }
            if ( s[i] == '\'' )
            {
                bInString = ( ! bInString ) ;
            }
        }
        //--- Build the array to return 
        if ( iCount > sep.length )
        {
            iCount = sep.length ;
        }
        int[] sep2 = new int[iCount];
        for ( int x = 0 ; x < sep2.length ; x++ )
        {
            sep2[x] = sep[x] ;
        }
        return sep2 ;
    }
    
    //--------------------------------------------------------------------------------
    /**
     * Returns the differents parts of the given string
     * @param s the string to cut 
     * @param cSeparator the separator character 
     * @param iMaxParts the maximum number of parts
     * @return the parts array (never null, can has 0 elements )
     */
    public static String[] getParts( String s, char cSeparator, int iMaxParts )
    {
        if ( s == null )
        {
            return new String[0];
        }
        
        int [] sep = getSeparators( s, cSeparator, iMaxParts-1 );
        int n = sep.length + 1 ;
        
        String part = null ;
        String[] parts = new String[n] ;
        int begin = 0 ;
        for ( int i = 0 ; i < parts.length ; i++ )
        {            
            begin = ( i > 0 ) ? sep[i-1] : 0 ;
            if ( i < sep.length )
            {
                part = s.substring(begin, sep[i]);
            }
            else
            {
                part = s.substring(begin);
            }
            if ( part.length() > 0 )
            {                
	            if ( part.charAt(0) == ',') // starts with ","
	            {
	                part = part.substring(1); // remove 1rst char
	            }
            }
            parts[i] = part.trim() ;
        }
        return parts ;        
    }
}
